/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ddsunase_parser;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.TreeMap;

/**
 *
 * @author samy
 */
public class DataRange_Reader {

    private TreeMap<Integer, TreeMap<Integer, DataRange>> mapResults = new TreeMap<Integer, TreeMap<Integer, DataRange>>();

    public DataRange_Reader(String filePath) {

        File file = new File(filePath);
        readMapFromCSV(file);

    }

    public void readMapFromCSV(File file) {

        mapResults.clear();
        BufferedReader br = null;
        String line = "";
        String cvsSplitBy = ",";

        DataRange deo;

        try {

            br = new BufferedReader(new FileReader(file));

            boolean firstTime = true;

            while ((line = br.readLine()) != null) {

                String[] values = line.split(cvsSplitBy);

                if (!firstTime) {
                    deo = new DataRange();
//
//                    deo.setNumOfComponent(Integer.parseInt(values[0]));
//                    deo.setNumOfVersionCount(Integer.parseInt(values[1]));
//                    deo.setNumOfAuthors(Integer.parseInt(values[2]));

                    deo.setNumberOfReports(Integer.parseInt(values[3-3]));
                    deo.setMinLimit(Integer.parseInt(values[4-3]));
                    deo.setMaxLimit(Integer.parseInt(values[5-3]));
                    deo.setMonth(Integer.parseInt(values[6-3]));
                    deo.setYear(Integer.parseInt(values[7-3]));

                    TreeMap<Integer, DataRange> monthMap = null;
                    if (mapResults.get(deo.getYear()) == null) {

                        monthMap = new TreeMap<Integer, DataRange>();
                        monthMap.put(deo.getMonth(), deo);

                    } else {

                        monthMap = mapResults.get(deo.getYear());
                        monthMap.put(deo.getMonth(), deo);

                    }

                    mapResults.put(deo.getYear(), monthMap);

                }

                firstTime = false;
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public TreeMap<Integer, TreeMap<Integer, DataRange>> getMapResults() {
        return mapResults;
    }

    public void setMapResults(
            TreeMap<Integer, TreeMap<Integer, DataRange>> mapResults) {
        this.mapResults = mapResults;
    }

}
